package com.renjibo.childred.fragments;


import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshExpandableListView;
import com.renjibo.childred.R;
import com.renjibo.childred.adapter.RecomMyExpandableAdapter;
import com.renjibo.childred.modle.RecomBrandsBean;
import com.renjibo.childred.utils.PostHead;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link RecomBrandsFragment#newInstance} factory method to
 * create an instance of this fragment.PullToRefreshBase.OnRefreshListener,ExpandableListView.OnGroupClickListener
 */
public class RecomBrandsFragment extends Fragment implements PullToRefreshBase.OnRefreshListener,ExpandableListView.OnGroupClickListener,View.OnClickListener{
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private PullToRefreshExpandableListView recom_pullToRefreshListView;
    private List<RecomBrandsBean.BrandBean> brand=new ArrayList<>();
    private RecomMyExpandableAdapter recomMyExpandableAdapter;
    private Button recom_button_return;


    public RecomBrandsFragment() {
    }


    public static RecomBrandsFragment newInstance(String param1, String param2) {
        RecomBrandsFragment fragment = new RecomBrandsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_recom_brands, container, false);
        initView(view);
        recomMyExpandableAdapter = new RecomMyExpandableAdapter(getActivity(), brand);
        resolveJson();
        return view;
    }
    public void initView(View view){
        recom_pullToRefreshListView = (PullToRefreshExpandableListView) view.findViewById(R.id.recom_pullToRefreshListView);
        recom_button_return = (Button) view.findViewById(R.id.recom_button_return);
        recom_pullToRefreshListView.getRefreshableView().setGroupIndicator(null);
        recom_pullToRefreshListView.getRefreshableView().setDivider(null);
        recom_pullToRefreshListView.getRefreshableView().setSelector(android.R.color.transparent);
        recom_pullToRefreshListView.getRefreshableView().setOnGroupClickListener(this);
        recom_pullToRefreshListView.setOnRefreshListener(this);
        recom_button_return.setOnClickListener(this);

        recom_pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        recom_pullToRefreshListView.getLoadingLayoutProxy(false, true).setPullLabel("刷新");
        recom_pullToRefreshListView.getLoadingLayoutProxy(false, true).setRefreshingLabel("setRefreshingLabel");
        recom_pullToRefreshListView.getLoadingLayoutProxy(false, true).setReleaseLabel("setReleaseLabel");
        ExpandableListView expandableListView = recom_pullToRefreshListView.getRefreshableView();

    }
    //请求数据
    public void resolveJson(){
        String url= PostHead.head+"/brand";
//        RequestParams params = new RequestParams(PostHead.head+url);
//        x.http().get(params, new Callback.CommonCallback<String>() {
        RequestParams params=new RequestParams(url);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d("zzz","re:"+result);
                RecomBrandsBean recomBrandsBean = PostHead.gson.fromJson(result, RecomBrandsBean.class);
                brand.addAll(recomBrandsBean.getBrand());
                Log.d("zzz","lenght:"+brand.size());
                recom_pullToRefreshListView.getRefreshableView().setAdapter(recomMyExpandableAdapter);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onRefresh(PullToRefreshBase refreshView) {
        Toast.makeText(getActivity(),"刷新",Toast.LENGTH_LONG);
//        recom_pullToRefreshListView.getRefreshableView().setAdapter(new MyBaseAdapter());
        recom_pullToRefreshListView.onRefreshComplete();
    }

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.recom_button_return:
//                Toast.makeText(getActivity(),"点击了按钮",Toast.LENGTH_SHORT).show();
                getActivity().getSupportFragmentManager().popBackStack();
                break;
        }
    }
}
